/*
package com.demo.servlet.word;

import com.aspose.words.Cell;
import com.aspose.words.*;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.Node;
import com.aspose.words.Paragraph;
import com.aspose.words.Row;
import com.aspose.words.Section;
import com.aspose.words.Shape;
import com.aspose.words.Table;
import com.aspose.words.net.System.Data.DataTable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class BasicWordController {
    private Document doc = null;

    static {
        try {
            License license = new License();
            license.setLicense(URLDecoder.decode(BasicWordController.class.getResource("Aspose.Words.lic").getFile(), "UTF-8"));
            if (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1 && System.getProperty("os.name").toLowerCase().indexOf("mac") == -1) {
                FontSettings.setFontsFolder("//usr//share//fonts", true);
            }

        } catch (Exception var1) {
            var1.printStackTrace();
            throw new RuntimeException(var1.getMessage());
        }
    }

    public BasicWordController() {
    }

    public Document openNewDocument() throws Exception {
        this.doc = new Document();
        return this.doc;
    }

    public Document openDocument(String docPath, String fileName) throws Exception {
        this.doc = new Document(docPath + fileName);
        return this.doc;
    }

    public Document openDocument(InputStream stream) throws Exception {
        this.doc = new Document(stream);
        return this.doc;
    }

    public Document getCurrentDocument() throws Exception {
        return this.doc;
    }

    public void setCurrentDocument(Document doc) throws Exception {
        this.doc = doc;
    }

    public void saveDocument(String docPath, String fileName, int saveFormat) throws Exception {
        this.doc.save(docPath + fileName, saveFormat);
    }

    public void saveDocument(OutputStream outputStream, int saveFormat) throws Exception {
        this.doc.save(outputStream, saveFormat);
    }

    public void protectDocument(String password) throws Exception {
        this.doc.protect(2, password);
    }

    public ByteArrayOutputStream outputStream() {
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            this.doc.save(os, 1);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return os;
    }

    public ByteArrayInputStream inputStream() {
        ByteArrayInputStream is = new ByteArrayInputStream(this.outputStream().toByteArray());
        return is;
    }

    public static byte[] getBytesFromFile(String filePath) {
        byte[] image = (byte[])null;
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(filePath);
            image = new byte[fis.available()];
            fis.read(image);
            fis.close();
        } catch (FileNotFoundException var14) {
            var14.printStackTrace();
        } catch (IOException var15) {
            var15.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException var13) {
                var13.printStackTrace();
            }

        }

        return image;
    }

    public void toWrite(String filePath) {
        InputStream in = null;
        FileOutputStream os = null;

        try {
            in = this.inputStream();
            os = new FileOutputStream(new File(filePath));
            Boolean bytesRead = false;
            byte[] buffer = new byte[8192];

            int bytesRead2 = 0;
            while((bytesRead2 = in.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead2);
            }
        } catch (Exception var14) {
            var14.printStackTrace();
        } finally {
            try {
                os.close();
                in.close();
            } catch (IOException var13) {
                var13.printStackTrace();
            }

        }

    }

    public Document mergeBeanList(MapListDataSource blds) throws Exception {
        this.doc.getMailMerge().executeWithRegions(blds);
        return this.doc;
    }

    public void mergeArray(String[] name, Object[] obj) throws Exception {
        this.doc.getMailMerge().execute(name, obj);
    }

    public void mergeSingleValue(String name, Object obj) throws Exception {
        this.mergeArray(new String[]{name}, new Object[]{obj});
    }

    public void mergeMap(Map<String, Object> map) throws Exception {
        String[] param = new String[map.size()];
        Object[] obj = new Object[map.size()];
        int i = 0;

        for(Iterator var6 = map.entrySet().iterator(); var6.hasNext(); ++i) {
            Entry<String, Object> entry = (Entry)var6.next();
            param[i] = (String)entry.getKey();
            obj[i] = entry.getValue();
        }

        this.mergeArray(param, obj);
    }

    public void mergeResultSet(ResultSet rs) throws Exception {
        this.doc.getMailMerge().executeWithRegions(new DataTable(rs));
    }

    public Table getTableByIndex(int tableIndex) throws Exception {
        return this.doc.getFirstSection().getBody().getTables().get(tableIndex);
    }

    public Node getNodeByName(String nodeName) throws Exception {
        DocumentBuilder builder = new DocumentBuilder(this.doc);
        Node node = null;
        if (builder.moveToMergeField(nodeName, false, false)) {
            node = builder.getCurrentParagraph().getNextSibling();
        }

        return node;
    }

    public Table getTableByName(String tableName) throws Exception {
        DocumentBuilder builder = new DocumentBuilder(this.doc);
        Table table = null;
        if (builder.moveToMergeField(tableName, false, false)) {
            table = (Table)builder.getCurrentParagraph().getNextSibling();
        }

        return table;
    }

    public Section getSectionByName(String nodeName) throws Exception {
        DocumentBuilder builder = new DocumentBuilder(this.doc);
        Section node = null;
        if (builder.moveToMergeField(nodeName, false, false)) {
            node = builder.getCurrentSection();
        }

        if (node == null) {
            throw new Exception("文档中没有" + nodeName + "的段落");
        } else {
            return node;
        }
    }

    public Row getRow(Table table, int rowIndex) throws Exception {
        Row row = null;
        if (table != null) {
            row = table.getRows().get(rowIndex);
        }

        return row;
    }

    public Row getRow(int tableIndex, int rowIndex) throws Exception {
        return this.getRow(this.getTableByIndex(tableIndex), rowIndex);
    }

    public Row getRow(String tableName, int rowIndex) throws Exception {
        return this.getRow(this.getTableByName(tableName), rowIndex);
    }

    public Cell getCell(Table table, int rowIndex, int cellIndex) throws Exception {
        return this.getCell(this.getRow(table, rowIndex), cellIndex);
    }

    public Cell getCell(Row row, int cellIndex) throws Exception {
        Cell cell = null;
        if (row != null) {
            cell = row.getCells().get(cellIndex);
        }

        return cell;
    }

    public Cell getCell(int tableIndex, int rowIndex, int cellIndex) throws Exception {
        return this.getRow(this.getTableByIndex(tableIndex), rowIndex).getCells().get(cellIndex);
    }

    public Cell getCell(String tableName, int rowIndex, int cellIndex) throws Exception {
        return this.getCell(this.getRow(this.getTableByName(tableName), rowIndex), cellIndex);
    }

    public void splitCell(Cell cell, int splitCount, String fillStr) throws Exception {
        if (cell != null) {
            if (fillStr == null || "".equals(fillStr)) {
                fillStr = "cell";
            }

            DocumentBuilder builder = new DocumentBuilder(this.doc);
            double cellWidth = cell.getCellFormat().getWidth() / (double)splitCount;
            Row row = cell.getParentRow();
            int rowNo = row.getParentTable().getRows().indexOf(row);
            int colNo = row.getCells().indexOf(cell);
            cell.remove();

            for(int n = 0; n < splitCount; ++n) {
                Cell newCell = new Cell(this.doc);
                newCell.getCellFormat().setWidth(cellWidth);
                newCell.getCellFormat().setVerticalAlignment(1);
                row.appendChild(newCell);
                Paragraph p = new Paragraph(this.doc);
                p.getParagraphFormat().setAlignment(1);
                newCell.appendChild(p);
                builder.moveTo(p);
                builder.insertField("MERGEFIELD " + fillStr + "_" + rowNo + "_" + (colNo + n) + " \\* MERGEFORMAT", "");
            }
        }

    }

    public void mergeCell(Cell cell, int horizontalMergeCount, int verticalMergeCount) throws Exception {
        if (horizontalMergeCount < 0) {
            throw new Exception("水平合并的单元格个数不能小于0！");
        } else if (verticalMergeCount < 0) {
            throw new Exception("垂直合并的单元格个数不能小于0！");
        } else {
            if (horizontalMergeCount + verticalMergeCount > 0) {
                if (horizontalMergeCount > 0) {
                    cell.getCellFormat().setHorizontalMerge(1);
                }

                if (verticalMergeCount > 0) {
                    cell.getCellFormat().setVerticalMerge(1);
                }

                Row row = cell.getParentRow();
                Table table = row.getParentTable();
                int rowNo = row.getParentTable().getRows().indexOf(row);
                int colNo = row.getCells().indexOf(cell);
                int rowCount = table.getRows().getCount();
                boolean hxhs = false;
                boolean lxhs = false;
                int i;
                int lxhs2;
                if (horizontalMergeCount > 0 && verticalMergeCount == 0) {
                    lxhs2 = row.getCount() - colNo - horizontalMergeCount > 0 ? horizontalMergeCount : row.getCount() - colNo;

                    for(i = colNo + 1; i < colNo + lxhs2; ++i) {
                        Cell c = row.getCells().get(i);
                        c.getCellFormat().setHorizontalMerge(2);
                    }
                }

                Cell c;
                int hxhs2;
                Row r;
                if (horizontalMergeCount == 0 && verticalMergeCount > 0) {
                    hxhs2 = rowCount - rowNo > verticalMergeCount ? verticalMergeCount : rowCount - rowNo;

                    for(i = rowNo + 1; i < rowNo + hxhs2; ++i) {
                        r = table.getRows().get(i);
                        c = r.getCells().get(colNo);
                        c.getCellFormat().setVerticalMerge(2);
                    }
                }

                if (horizontalMergeCount > 0 && verticalMergeCount > 0) {
                    hxhs2 = rowCount - rowNo > verticalMergeCount ? verticalMergeCount : rowCount - rowNo;

                    for(i = rowNo; i < rowNo + hxhs2; ++i) {
                        r = table.getRows().get(i);
                        lxhs2 = r.getCount() - colNo - horizontalMergeCount > 0 ? horizontalMergeCount : r.getCount() - colNo;

                        for(int j = colNo; j < colNo + lxhs2; ++j) {
                            Cell c = r.getCells().get(j);
                            if (i != rowNo || j != colNo) {
                                c.getCellFormat().setHorizontalMerge(2);
                            }
                        }
                    }

                    for(i = rowNo + 1; i < rowNo + hxhs2; ++i) {
                        r = table.getRows().get(i);
                        c = r.getCells().get(colNo);
                        c.getCellFormat().setVerticalMerge(2);
                    }
                }
            }

        }
    }

    public void addImageWatermark(String imageFilePath, String imageFileName) throws Exception {
        DocumentBuilder builder = new DocumentBuilder(this.doc);
        builder.moveToHeaderFooter(1);
        Shape shape = builder.insertImage(imageFilePath + imageFileName);
        shape.setWrapType(3);
        shape.setBehindText(true);
        shape.setZOrder(5);
        shape.setAllowOverlap(true);
        shape.setRelativeHorizontalPosition(1);
        shape.setHorizontalAlignment(2);
        shape.setRelativeVerticalPosition(1);
        shape.setVerticalAlignment(2);
        builder.moveToHeaderFooter(3);
    }

    public void addImage(String imageFilePath, String imageFileName) throws Exception {
        DocumentBuilder builder = new DocumentBuilder(this.doc);
        builder.moveToDocumentEnd();
        builder.insertBreak(1);
        Shape shape = builder.insertImage(imageFilePath + imageFileName);
        shape.setWrapType(3);
        shape.setBehindText(true);
        if (shape.getWidth() > builder.getPageSetup().getPageWidth()) {
            shape.setWidth(builder.getPageSetup().getPageWidth() - 10.0D);
        }

        if (shape.getHeight() > builder.getPageSetup().getPageHeight()) {
            shape.setHeight(builder.getPageSetup().getPageHeight() - 10.0D);
        }

        shape.setRelativeHorizontalPosition(1);
        shape.setHorizontalAlignment(2);
        shape.setRelativeVerticalPosition(1);
        shape.setVerticalAlignment(2);
    }

    public void addPDF(String pdfFilePath, String pdfFileName) throws Exception {
        DocumentBuilder builder = new DocumentBuilder(this.doc);
        builder.moveToHeaderFooter(1);
        Shape shape = builder.insertImage(pdfFilePath + pdfFileName);
        shape.setWrapType(3);
        shape.setBehindText(true);
        shape.setZOrder(5);
        shape.setAllowOverlap(true);
        shape.setRelativeHorizontalPosition(1);
        shape.setHorizontalAlignment(2);
        shape.setRelativeVerticalPosition(1);
        shape.setVerticalAlignment(2);
        builder.moveToHeaderFooter(3);
    }

    public void fillMergeField(ResultSet rs) throws Exception {
        this.doc.getMailMerge().execute(new DataTable(rs));
    }

    public void fillMergeField(String tableName, ResultSet rs) throws Exception {
        this.doc.getMailMerge().executeWithRegions(new DataTable(rs, tableName));
    }

    public void deleteParagraph(String paragraphName) {
    }

    public void deleteMegreField() {
        try {
            this.doc.getMailMerge().deleteFields();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteTablesOfSpace(String startTag, String endTag) throws Exception {
        Document doc = this.getCurrentDocument();
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.moveToMergeField(startTag, false, false);
        Node nodeStart = builder.getCurrentParagraph();
        builder.moveToMergeField(endTag, false, false);
        Node nodeEnd = builder.getCurrentParagraph();
        int start = nodeStart.getParentNode().indexOf(nodeStart);
        int end = nodeEnd.getParentNode().indexOf(nodeEnd);

        for(int i = end - 1; i >= start + 1; --i) {
            Node temp = nodeStart.getParentNode().getChildNodes().get(i);
            if (temp != null && 8 == temp.getNodeType()) {
                temp.remove();
            }
        }

    }
}
*/
